package com.nhimeye.admin.ui;

import java.util.HashSet;

import org.springframework.beans.factory.annotation.Autowired;

import com.nhimeye.admin.app.AbstractEntityView;
import com.nhimeye.admin.app.EntityEditor;
import com.nhimeye.admin.app.EntityProviderUtil;
import com.nhimeye.admin.app.H1;
import com.nhimeye.domain.ContentVersion;
import com.nhimeye.domain.FieldConfiguration;
import com.nhimeye.service.FieldConfigurationService;
import com.vaadin.addon.jpacontainer.EntityItem;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.spring.roo.addon.annotations.RooVaadinEntityView;
import com.vaadin.ui.Button;
import com.vaadin.ui.Table;


public class FieldConfigurationView extends AbstractEntityView<FieldConfiguration> {
	@Autowired
	FieldConfigurationService fieldConfigurationService;
	
	private JPAContainer<FieldConfiguration> tableContainer;
	
    @Override
    protected void configureTable(Table table) {
        table.setContainerDataSource(getTableContainer());
        table.setVisibleColumns(getTableColumns());

        
    }

	@Override
	protected HashSet<String> getIgnoreTableColumns() {
		return new HashSet<String>();
	}

	@Override
	protected Class<? extends FieldConfiguration> getEntityClass() {
		 return FieldConfiguration.class;
	}

	@Override
	public boolean isCreateAllowed() {
		  return true;
	}

	@Override
	protected boolean isUpdateAllowed() {
		  return true;
	}

	@Override
	protected boolean isDeleteAllowed() {
		  return true;
	}

	

	@Override
	protected void deleteEntity(FieldConfiguration entity) {
		if (entity != null) {
			fieldConfigurationService.deleteFieldConfiguration(entity);
	        }
	}
	
	@Override
	public Object getIdProperty() {
		return "id";
	}

	@Override
	public Object getVersionProperty() {
		return "version";
	}

	@Override
	protected Container getTableContainer() {
		if (tableContainer == null) {
            JPAContainer<FieldConfiguration> container = new JPAContainer<FieldConfiguration>(FieldConfiguration.class);
            container.setEntityProvider(EntityProviderUtil.get().getEntityProvider(FieldConfiguration.class));
            tableContainer = container;
        }
        return tableContainer;
	}

	@Override
	protected Item getItemForEntity(FieldConfiguration entity) {
		Item item = getTable().getItem(entity.getId());
        if (item == null) {
            item = tableContainer.createEntityItem(entity);
        }
        return item;	
        }

	@Override
	protected FieldConfiguration getEntityForItem(Item item) {
		 if (item instanceof EntityItem && ((EntityItem) item).getEntity() instanceof FieldConfiguration) {
	            return (FieldConfiguration) ((EntityItem) item).getEntity();
	        } else {
	            return null;
	        }
	}

	@Override
	public Object getIdForEntity(FieldConfiguration entity) {
		return entity != null ? entity.getId() : null;
    }
	

	@Override
	protected FieldConfiguration createEntityInstance() {
		return new FieldConfiguration();
	}

	@Override
	protected void configureButton(Button b) {
		b.setCaption("+ Add FieldConfiguration");
		
	}

	@Override
	protected void configureCaption(H1 title) {
		title.setCaption("FieldConfigurations");
	}

	@Override
	protected boolean isNewEntity(FieldConfiguration entity) {
		 return (entity != null && getIdForEntity(entity) == null);
    }
	

	@Override
	protected EntityEditor<FieldConfiguration> createForm() {
		return new FieldConfigurationForm();
	}

	@Override
	protected String getEntityName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected FieldConfiguration saveEntity(FieldConfiguration entity) {
		// TODO Auto-generated method stub
		return null;
	}

}
